package sk.marek.barak.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextArea;

public class ComentUI extends Composite {
	private String user;
	private String key;
	private String text;
	private String userId;
	
	public ComentUI(Coment coment) {
		user = coment.getUser();
		key = coment.getKey();
		text = coment.getText();
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("169px", "67px");
		
		Label lblUser = new Label(user);
		absolutePanel.add(lblUser, 0, 0);
		
		PushButton pshbtnClose = new PushButton("x");
		absolutePanel.add(pshbtnClose, 150, 0);
		pshbtnClose.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				if(ShareLife.getCurentUser().getUserID()==userId){
				ComentServiceAsync comentService =GWT.create(ComentService.class);
				comentService.removeComent(key, new AsyncCallback<Void>() {
					
					@Override
					public void onSuccess(Void result) {
						removeFromParent();
						
					}
					
					@Override
					public void onFailure(Throwable caught) {
						Window.alert("Failed to remove");
						
					}
				});
				
			}else
				Window.alert("You cant delete this comment");
			}
		});
		pshbtnClose.setSize("7px", "11px");
		
		TextArea textArea = new TextArea();
		textArea.setText(text);
		absolutePanel.add(textArea, 0, 19);
	}
}
